package com.zhou.test.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.*;

import javax.swing.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class MvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private SessionHandlerInterceptor sessionHandlerInterceptor;

    @Value("${file.path}")
    private String filePath;

    //解决跨域的问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Autowired
    //格式化
    public void addFormatters(FormatterRegistry registry) {
        // 对于日 期类型的参数， Spring 默认并没有配置如何将字符串转为日期类型。为了支持可按照指定格式转为日期类型，需要添加一个 DateFormatter 类
        registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }

    // URI 到视图的映射
    public void addViewControllers(ViewControllerRegistry registry) {
        // 对于 index.html 的请求，设置返回的视图为 index.ht! 。
        registry.addViewController("/index.html").setViewName("/indexbtl");
        // 所有以 .do 结尾的请求重定向到／index.html 请求
        registry.addRedirectViewController("/**/*.do", "/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {//将访问路径映射到磁盘路径
        registry.addResourceHandler("/file/**").addResourceLocations(filePath);
    }

    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //  将加一个拦截器，检查会话， URL 以 admin 开头的都使用此拦截器
        registry.addInterceptor(sessionHandlerInterceptor).addPathPatterns("/admin/**");

    }

    // jackson的全局配置 日期和long变成string(解决mybatis-plus 生成主健到前台失去精度的问题)
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer builderCustomizer() {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeSerializeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateTimeDeserializeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        return builder -> {
            // 所有Long类型转换成String到前台
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            builder.serializerByType(LocalDateTime.class, new LocalDateSerializer(dateTimeSerializeFormatter));
        };
    }


}